package messenger;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Logger;

public class MessengerClient {
	private MessengerSession session = null;
	private Logger log = Logger.getLogger("MessengerClient");
	private static final int TIMEOUT = 30000;
	
	public static void main(String[] args) {
		MessengerClient client = new MessengerClient();
		client.connect("localhost", 8081, "zjy", "zjy", false, null,0, null);
		client.disconnect();
	}
	
	public MessengerClient() {
	}
	
	public boolean connect(String host, int port, String userName, String password, boolean useProxy, String proxyHost, int proxyPort, String proxyTypeStr) {
		log.info("Connect to MessengerServer");
		Socket socket;
		boolean isHttpProxy = false;
		try {
			if(!useProxy) {
				socket = new Socket(host, port);
				socket.setSoTimeout(TIMEOUT);
			}else {
				if("HTTP".equals(proxyTypeStr)) {
					isHttpProxy = true;
					socket = new Socket();
					socket.setSoTimeout(TIMEOUT);
					socket.connect(new InetSocketAddress(proxyHost, proxyPort), 10000);
				}else {
					Proxy.Type proxyType = Proxy.Type.SOCKS;
				
					Proxy proxy = new Proxy(proxyType, new InetSocketAddress(proxyHost, proxyPort));
					
					socket = new Socket(proxy);
					socket.setSoTimeout(TIMEOUT);
					socket.connect(new InetSocketAddress(host, port), TIMEOUT);
				}
			}
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}		
		session = new MessengerSession(socket, userName, isHttpProxy, host, port);
		log.info("Connect successfully");
		if(!session.sendMessage(MessengerSession.A_LOGIN,userName+MessengerSession.N_SPLIT2+password+MessengerSession.N_SPLIT2+isHttpProxy, "$SERVER$")) {
			log.info("Send message failed");
			return false;
		}
		String msg = receiveMessage();
		if(msg == null || msg.trim().length() == 0) {
			log.info("Server has no respose!");
			return false;
		}
		String[] tmps = msg.split(MessengerSession.N_SPLIT1);
		if(tmps.length != 5) {
			log.info("Invalid message");
			return false;
		}
		String action = tmps[0];
		String from = tmps[2];
		String type = tmps[3];
		String message = tmps[4];
		if(action.equals(MessengerSession.A_LOGIN) && from.equals(MessengerSession.N_SERVER) &&
				type.equals(MessengerSession.T_COMMAND) && message.equals(MessengerSession.R_SUCCEED)) {
			log.info("Login successfully");
			return true;
		}else {
			log.info("Login failed");
			return false;
		}
	}
	
	public void signalGetFriendList() {
		session.sendMessage(MessengerSession.A_GET_FRIEND, "signalGetFriendList", MessengerSession.N_SERVER);
	}
	
	public boolean sendMessage(String msg, String to) {
		return session.sendMessage(MessengerSession.A_SEND_MESSAGE,msg,to);
	}
	
	public void disconnect() {
		log.info("Disconnect from MessengerServer");
		session.sendMessage(MessengerSession.A_DISCONNECT,"disconnect", "$SERVER$");
		session.closeSession();
		log.info("Disconnect from MessengerServer successfully");
	}
	
	public String receiveMessage() {
		return session.receiveMessage();
	}
	
	public String getUser() {
		return this.session.getUser();
	}
	
	public boolean isValidMessage(String msg) {
		return session.isValidMessage(msg);
	}
	
}
